package com.hdd.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzh.sshop.admin.service.LoginService;

import com.hdd.admin.entity.Menu;
import com.hdd.admin.entity.User;
import com.hdd.admin.mapper.DeptMapper;
import com.hdd.admin.mapper.MenuMapper;
import com.hdd.admin.mapper.UserMapper;
import com.hdd.admin.req.LoginRequest;
import com.hdd.admin.service.MenuService;
import com.hdd.config.jwt.JWTUtil;
import com.hdd.dto.SuperResponse;
import com.hdd.utils.MD5Util;
import com.hdd.utils.wx.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisUtil myRedisUtil;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuService menuService;

    private static Long expireMillis = 360*60*1000L;

    @Override
    public SuperResponse userLogin(LoginRequest request) {
        SuperResponse response = new SuperResponse();

        //TODO 应为后台系统基本是内部使用, 简单的做了一个 验证码校验
        if(!myRedisUtil.hasKey("VCode:"+request.getVerifyCode().toUpperCase())){
            response.setCode(-2000);
            response.setMessage("验证码有误!");
            return response;
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", request.getAccount());
        User user = userMapper.selectOne(queryWrapper);
        if(null==user){
            response.setCode(-1001);
            response.setMessage("账号或密码错误");
            return response;
        }

        if(MD5Util.getMD5Format(request.getPassword()).equals(user.getPassword())){
            response.setCode(-1002);
            response.setMessage("登录密码有误");
            return response;
        }

        List<Menu> menuList =  menuMapper.findMenuByUid(user.getUserId());
        for(Menu m:menuList){
            m.setChildren(menuService.findChildren(m.getMenuId(),user.getUserId()));
        }
        user.setMenus(menuList);
        //TODO 待优化
        response.setToken(JWTUtil.createJWT(expireMillis,user.getAccount()));
        user.setPassword(null);
        user.setDeptName(deptMapper.selectById(user.getDeptId()).getDeptName());
        response.setItem(user);
        return response;
    }


    @Override
    public SuperResponse mobileLogin(LoginRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", request.getAccount());
        User user = userMapper.selectOne(queryWrapper);
        if(null==user){
            response.setCode(-1001);
            response.setMessage("账号或密码错误");
            return response;
        }

        if(MD5Util.getMD5Format(request.getPassword()).equals(user.getPassword())){
            response.setCode(-1002);
            response.setMessage("登录密码有误");
            return response;
        }

        if(!"1".equals(user.getStatus().toString())){
            response.setCode(-1002);
            response.setMessage("该帐号已暂停使用");
            return response;
        }

        response.setItem(user);
        return response;
    }
}
